package com.yixin.hospital.controller;

import com.yixin.hospital.common.ApiResult;
import com.yixin.hospital.common.Constant;
import com.yixin.hospital.service.MessageService;
import com.yixin.hospital.utils.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/message")
@Api(value = "通知管理", tags = "通知管理")
@Slf4j
public class MessageController {

    @Resource
    private MessageService messageService;

    @GetMapping("/user-message-type")
    @ApiOperation(value = "消息类型列表")
    public ApiResult<?> userMessageType(HttpServletRequest request) {
        Long userId = JwtUtil.getUserId(request.getHeader(Constant.AUTHORIZATION));
        return messageService.userMessageType(userId);
    }

    @GetMapping("/user-message-by-type")
    @ApiOperation(value = "消息列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "messageType", value = "消息类型id", paramType = "query"),
    })
    public ApiResult<?> userMessageByType(@RequestParam int currentPage, @RequestParam int pageSize,
                                          @RequestParam(required = false) String messageType, HttpServletRequest request) {
        Long userId = JwtUtil.getUserId(request.getHeader(Constant.AUTHORIZATION));
        return messageService.userMessageByType(currentPage, pageSize, messageType, userId);
    }

    @PostMapping("/message-read")
    @ApiOperation(value = "消息已读")
    public ApiResult<?> messageRead(Long id, HttpServletRequest request) {
        String name = JwtUtil.getName(request.getHeader(Constant.AUTHORIZATION));
        return messageService.messageRead(id, name);
    }

    @PostMapping("/message-all-read")
    @ApiOperation(value = "消息全部已读")
    public ApiResult<?> messageAllRead(HttpServletRequest request) {
        Long userId = JwtUtil.getUserId(request.getHeader(Constant.AUTHORIZATION));
        return messageService.messageAllRead(userId);
    }

    @DeleteMapping("/remove/{id}")
    @ApiOperation(value = "删除消息")
    public ApiResult remove(@PathVariable int id) {
        messageService.removeById(id);
        return ApiResult.success();
    }

}
